package com.auto.uhoo.ui.base;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import com.auto.uhoo.tools.ToastHelper;
import com.auto.uhoo.ui.view.IView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment implements IView {

    private Context context;

    private View rootView;
    private View contentView;
    private FrameLayout containerView;
    private Unbinder unbinder;

    public BaseFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        int layoutId = getLayoutId();
        if (layoutId > 0){
            contentView = inflater.inflate(layoutId, null, false);
            unbinder = ButterKnife.bind(this, contentView);

            containerView = new FrameLayout(context);
            LinearLayout tmpContainer = new LinearLayout(context);
            tmpContainer.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            contentView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            tmpContainer.addView(contentView);

            containerView.addView(tmpContainer);
            rootView = containerView;

            Bundle bundle = getArguments();
            initResource(bundle);
            initViews(tmpContainer, bundle);

            return containerView;
        }

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    //获取布局ID
    protected abstract int getLayoutId();
    //初始化布局
    protected abstract void initViews(View view, Bundle bundle);
    //初始化资源
    protected abstract void initResource(Bundle bundle);
    @Override
    public void showShortToast(String content) {
        ToastHelper.showShortToast(getActivity(), content);
    }

    @Override
    public void showLongToast(String content) {
        ToastHelper.showLongToast(getActivity(), content);
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void hideLoadingDialog() {

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null)
            unbinder.unbind();
    }
}
